package cn.com.xyc.listener;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.AbstractMessageConverter;
import org.springframework.amqp.support.converter.MessageConversionException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class FastJsonMessageConverter extends AbstractMessageConverter {
	public static final String DEFAULT_CHARSET = "UTF-8";

	private volatile String defaultCharset = DEFAULT_CHARSET;

	@Override
	protected Message createMessage(Object arg0,
			MessageProperties messageProperties) {
		ObjectMapper objectMapper = new ObjectMapper();
		byte[] objectBytes = null;
		try {
			objectBytes = objectMapper.writeValueAsBytes(arg0);
			messageProperties
					.setContentType(MessageProperties.CONTENT_TYPE_JSON);
			messageProperties.setContentEncoding(this.defaultCharset);
			if (objectBytes != null) {
				messageProperties.setContentLength(objectBytes.length);
			}
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return new Message(objectBytes, messageProperties);
	}

	@Override
	public Object fromMessage(Message arg0) throws MessageConversionException {
		// TODO Auto-generated method stub
		return null;
	}

}
